/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sunrise.boundary;

import com.sunrise.entity.Event;
import com.sunrise.entity.User;
import com.sunrise.entity.WeatherAlert;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author Andrea
 */

@Stateless
public class WeatherAlertManager extends GenericManager<WeatherAlert> {

    @PostConstruct
    private void init() {
        super.typeParameterClass = WeatherAlert.class;
    }
    
    public boolean delete(WeatherAlert weatherAlert) {
        try {
            WeatherAlert persistedEntity = entityManager.getReference(WeatherAlert.class, weatherAlert.getId());
            entityManager.remove(persistedEntity);
        } catch (TransactionRequiredException | IllegalArgumentException ex) {
            return false;
        }
        
        return true;
    }
    
    public List<WeatherAlert> findWeatherAlertsForRecipient(User recipient) {
        Map params = new HashMap<>();
        params.put("recipient", recipient);
        return super.findManyWithNamedQuery(WeatherAlert.findByRecipient, params);
    }
    
    public List<WeatherAlert> findWeatherAlertsForEvent(Event event) {
        Map params = new HashMap<>();
        params.put("event", event);
        return super.findManyWithNamedQuery(WeatherAlert.findByEvent, params);
    }
}
